package com.lpc.kotlintest

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.properties.Delegates
import kotlin.reflect.KProperty

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

//        var student = Student("lpc")
//        tv.text = student.name

        val student: Student
//        student?.name = "yu"
        student = Student("lipc")
        tv.text = student?.name ?: "123"

        var v = View.OnClickListener { Log.e("lipc0113", "click") }
        tv.setOnClickListener(v)

        tv.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {

            }


        })

        setTest2(object : Test() {

        })

        var type = EnumTest.WHITE
        val ordinal = type.ordinal
        val name = type.name
        Toast.makeText(this, "$ordinal, $name, ${EnumTest.valueOf("GREEN")}", Toast.LENGTH_SHORT).show()

        val t = Test()
        Test2(t).test()
    }

    fun parseInt(str: String): Int? {

        var s = str?.length

        var ss = str ?: "123"

        return str.toIntOrNull()
    }

    fun testFor(): Unit {
        val listOf = listOf("1", "2", "3")

        for (s in listOf) {
            println(s)
        }

        for (index in listOf.indices) {
            println(listOf[index])
        }
    }

    fun testWhen(obj: Any) {

        when (obj) {

            1 -> "1"

            "2" -> "2"

            3 -> {

            }
        }
    }

    data class Student(var name: String) {
        var id: Int = 0
    }

    var test: Test? = null


    fun setTest2(test2: Test) {
        test = test2
    }

    open class Test() {
        open fun test() {}
    }

    class Test2(t: Test) : Test();

    enum class EnumTest(var i: Int) {

        WHITE(1),
        BLACK(1),
        GREEN(1)
    }

}
